package com.gmail.theredfaceone.cp.client;

import com.gmail.theredfaceone.redface.lib.RFUI;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class UI extends JFrame{

    private boolean isLoggingIn = true;
    //Login UI Components
    private final JTextField address = new JTextField("Address...");
    private final JTextField port = new JTextField("Port...");
    private final JTextField pw = new JTextField("Password...");
    private final JButton login = new JButton("Login");
    private final JLabel error = new JLabel();

    //Control Panel UI Components
    private final JTabbedPane cptabs = new JTabbedPane();
    // CP Tabs
    private final Container console = new Container();
    //  Console components
    private final Container server = new Container();
    //  Server components
    private final Container players = new Container();
    //  Players components
    private final Container plugins = new Container();
    //  Plugins components
    //Network shit
    Socket socket = null;

    public UI(){
        this.initLayout();
        this.initListeners();
        this.setSize(480, 270);
    }

    //Initialize layout for login window.
    private void initLayout(){
        this.setVisible(true);
        this.setTitle("Login");
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setIconImage(new ImageIcon(UI.class.getResource("resource/Redface_ico128x.png")).getImage());
        address.setLocation(0, 10);
        RFUI.resizeYProportional(address, this.getContentPane(), 10);
        this.add(address);
        RFUI.resizeYProportional(port, this.getContentPane(), 10);
        RFUI.putXBelow(port, address, 20);
        this.add(port);
        RFUI.resizeYProportional(pw, this.getContentPane(), 10);
        RFUI.putXBelow(pw, port, 20);
        this.add(pw);
        RFUI.resizeYProportional(login, this.getContentPane(), 15);
        RFUI.putXBelow(login, pw, 20);
        this.add(login);
        error.setLocation(0, 200);
        error.setForeground(Color.RED);
        error.setVisible(false);
        this.add(error);
    }

    //Initialize listeners for login window.
    private void initListeners(){
        login.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                isLoggingIn = false;
                setSize(960, 540);
                remove(address);
                remove(port);
                remove(pw);
                remove(login);
                repaint();
                setTitle("Control Panel");
                cpInitLayout();
                cpInitListeners();
            }
        });
        this.addComponentListener(new ComponentListener(){
            @Override
            //Dynamically resizes components when window is resized.
            public void componentResized(ComponentEvent ce){
                if(isLoggingIn){
                    RFUI.expandX(address, getContentPane(), 10, 10);
                    RFUI.expandX(port, getContentPane(), 10, 10);
                    RFUI.expandX(pw, getContentPane(), 10, 10);
                    RFUI.expandX(login, getContentPane(), 150, 150);
                    RFUI.resizeYProportional(address, getContentPane(), 10);
                    RFUI.resizeYProportional(port, getContentPane(), 10);
                    RFUI.putXBelow(port, address, 20);
                    RFUI.resizeYProportional(pw, getContentPane(), 10);
                    RFUI.putXBelow(pw, port, 20);
                    RFUI.resizeYProportional(login, getContentPane(), 15);
                    RFUI.putXBelow(login, pw, 20);
                }

            }

            @Override
            public void componentMoved(ComponentEvent ce){

            }

            @Override
            public void componentShown(ComponentEvent ce){

            }

            @Override
            public void componentHidden(ComponentEvent ce){

            }
        });
        //Clears text fields when they gain focus if they only contain the default text. Also puts the default text back if they are empty when they lose focus.
        address.addFocusListener(new FocusListener(){

            @Override
            public void focusGained(FocusEvent fe){
                if(address.getText().equals("Address...")){
                    address.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent fe){
                if(address.getText().equals("")){
                    address.setText("Address...");
                }
            }

        });
        port.addFocusListener(new FocusListener(){

            @Override
            public void focusGained(FocusEvent fe){
                if(port.getText().equals("Port...")){
                    port.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent fe){
                if(port.getText().equals("")){
                    port.setText("Port...");
                }
            }

        });
        pw.addFocusListener(new FocusListener(){

            @Override
            public void focusGained(FocusEvent fe){
                if(pw.getText().equals("Password...")){
                    pw.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent fe){
                if(pw.getText().equals("")){
                    pw.setText("Password...");
                }
            }

        });
    }

    //Initialize layout for control panel.
    private void cpInitLayout(){
        cptabs.setLocation(0, 0);
        cptabs.setSize(960, 540);
        this.add(cptabs);
        cptabs.addTab("Console", console);
        console.setSize(cptabs.getWidth(), cptabs.getHeight());
        cptabs.addTab("Server", server);
        server.setSize(cptabs.getWidth(), cptabs.getHeight());
        cptabs.addTab("Players", players);
        players.setSize(cptabs.getWidth(), cptabs.getHeight());
        cptabs.addTab("Plugins", plugins);
        plugins.setSize(cptabs.getWidth(), cptabs.getHeight());
    }

    //Initialize listeners for control panel.
    private void cpInitListeners(){

    }

}
